Imports obout_ASPTreeView_Pro_NET
Imports ccapps.persistent.authorization
'Imports ccapps.persistent.laLocationDefine
Imports ccapps.persistent.common
Imports ccapps.persistent.RetantionActivate
Imports System.IO
Imports ccapps.persistent.batchJobRequest
Imports ccapps.Util

Namespace Web.page

    Public Class TEBUnassignedFreeUnitReport
        Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents TreeView As System.Web.UI.WebControls.Literal
        Protected WithEvents chk_a0 As System.Web.UI.WebControls.CheckBox
        Protected WithEvents ltlAlert As System.Web.UI.WebControls.Literal
        Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater
        Protected WithEvents proc As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents userid As System.Web.UI.WebControls.TextBox
        Protected WithEvents username As System.Web.UI.WebControls.TextBox
        Protected WithEvents Txt1 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents Lbl_Custcode As System.Web.UI.WebControls.Label
        Protected WithEvents Lblok As System.Web.UI.WebControls.Label
        Protected WithEvents Lbl_Coid As System.Web.UI.WebControls.Label
        Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
        Protected WithEvents BtnExcel As System.Web.UI.WebControls.Button

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

        Public Sub ASPNET_MsgBox(ByVal Message As String)

            System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)

            System.Web.HttpContext.Current.Response.Write("alert(""" & Message & """)" & vbCrLf)

            System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

        End Sub


#End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Me.PageTitle = "TEB Unassigned Free Unit Report"

            If Not Page.IsPostBack Then
                Me.loadForm()
            End If

        End Sub

        Private Sub loadForm()
            Session("_detailRows") = Nothing

            Me.DataGrid1.DataSource = DetailRows
            Me.DataGrid1.DataBind()
        End Sub

        Private ReadOnly Property DetailRows() As DataTable
            Get
                Dim _detailRows As DataTable = Session("_detailRows")
                If _detailRows Is Nothing Then

                    Dim sql As String
                    Dim dt As DataTable

                    ' Begin : BSCS_IX_R3 Version (New)

                    sql = "    select /*+use_nl(a b c d)*/ b.custcode, c.custcode, c.customer_id, d.co_id, fn_coid_to_msisdn(d.co_id) MSISDN, " & _
                                  vbNewLine & "                  nvl( " & _
                                  vbNewLine & "                             (select max(to_char(prm_valid_from, 'dd.mm.yyyy')) " & _
                                  vbNewLine & "                                   from parameter_value " & _
                                  vbNewLine & "                                   where prm_value_id in (select x.prm_value_id from profile_service x where x.co_id = d.co_id and sncode = 53)" & _
                           vbNewLine & "                                         and prm_valid_from < sysdate " & _
                           vbNewLine & "                                         and parameter_id = 34 " & _
                           vbNewLine & "                                         and prm_value_number = 442), " & _
                           vbNewLine & "                             'Yok') Free_Unit_442," & _
                           vbNewLine & "                  (select ch_validfrom from contract_history x where x.co_id = d.co_id and x.ch_seqno = 2 and x.ch_status = 'a') Activation_date," & _
                           vbNewLine & "                  (select ch_validfrom from contract_history x where x.co_id = d.co_id and x.ch_status = 'd') Deactivation_date " & _
                                vbNewLine & " from promo_assign a, customer_all b, customer_all c, contract_all d  " & _
                           vbNewLine & "      where pack_id = 249 " & _
                           vbNewLine & "            and a.customer_id = b.customer_id  " & _
                           vbNewLine & "            and c.custcode like b.custcode || case substr(b.custcode, 1, 1)  " & _
                    vbNewLine & "                                                          when '1' then ''  " & _
                                  vbNewLine & "                                             else '%'  " & _
                                              vbNewLine & "                      end  " & _
                           vbNewLine & " and c.customer_id = d.customer_id "

                    ' End : BSCS_IX_R3 Version (New)

                    _detailRows = persistent.myDBConn.myORATable(sql)
                    Session("_detailRows") = _detailRows
                End If
                Return _detailRows
            End Get
        End Property




        Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
            Me.DataGrid1.CurrentPageIndex = e.NewPageIndex
            Me.DataGrid1.DataSource = DetailRows()
            Me.DataGrid1.DataBind()
        End Sub

        Private Sub BtnExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExcel.Click

            Dim raporDataTable As DataTable = DetailRows()
            Dim oView As New DataView(raporDataTable)
            DataGrid1.DataSource = oView

            Dim currentindex As Integer = Me.DataGrid1.CurrentPageIndex
            DataGrid1.AllowPaging = False
            DataGrid1.DataBind()

            Dim strFileName As String = "TEBUnassignedFreeUnitReport-" & Now.Today.ToString("yyyy-MM-dd") & ".xls"
            Response.Clear()
            Response.Buffer = True
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = ""
            EnableViewState = False

            Response.AddHeader("Content-Disposition", "attachment; filename=""" & strFileName & """")
            'Response.ContentEncoding = System.Text.Encoding.UTF7

            Dim oStringWriter As New System.IO.StringWriter
            Dim oHTMLTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

            DataGrid1.RenderControl(oHTMLTextWriter)
            Response.Write(oStringWriter.ToString())
            Response.End()

            DataGrid1.AllowPaging = True
            Me.DataGrid1.CurrentPageIndex = currentindex


        End Sub
    End Class

End Namespace